package com.liangzhong.modules.winebar.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.ModulePanelTypeConst;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.events.WinebarEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxyconstant.winebar.WinebarConst;
	import com.lz.football.module.wineBar.IWineBarView;
	
	import flash.display.DisplayObject;
	
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *酒吧 
	 * @author lake
	 * 
	 */	
	public class WineBarMediator extends ModuleMediator
	{
		public static const MediatorName :String = "WineBarMediator";
		
		public function WineBarMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		/**
		 *获取视图 
		 * @return 
		 * 
		 */		
		public function get view():IWineBarView
		{
			return viewComponent as IWineBarView;
		}
		/**
		 * 初始化
		 * 
		 */		
		override public function initMediator():void
		{
			addEventListeners();
		}
		/**
		 * 
		 * 添加事件
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).addEventListener(WinebarEvent.GRADE_UP,gradeUpHandler);
			(view as DisplayObject).addEventListener(WinebarEvent.LEAVE,leaveHandler);
			(view as DisplayObject).addEventListener(ModuleEvent.DROP_TIME,dropTimeHandler);
		}
		/**
		 * 删除事件
		 * 
		 */		
		override public function removeEventListeners():void
		{
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).removeEventListener(WinebarEvent.GRADE_UP,gradeUpHandler);
			(view as DisplayObject).removeEventListener(WinebarEvent.LEAVE,leaveHandler);
			(view as DisplayObject).removeEventListener(ModuleEvent.DROP_TIME,dropTimeHandler);
		}
		/**
		 * 删除模块
		 * @param event
		 * 
		 */
		private function onRemoveModuleHandler(e:ModuleEvent):void
		{
			removeMediator();
		}
		/**
		 *升级
		 * @param e
		 * 
		 */		
		private function gradeUpHandler(e :WinebarEvent):void
		{
			this.sendNotification(ApplicationConstants.WINEBAR_GRADE_UP);
		}
		/**
		 *去除冷却时间 
		 * @param e
		 * 
		 */		
		private function dropTimeHandler(e :ModuleEvent):void
		{
			this.sendNotification(ApplicationConstants.Public_Drop_Time,e.param);
		}
		/**
		 *离开结算 
		 * @param e
		 * 
		 */		
		private function leaveHandler(e :ModuleEvent):void
		{
			this.sendNotification(ApplicationConstants.WINEBAR_LEAVE,e.param);
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			super.removeMediator();
			facade.removeMediator(MediatorName);
			this.sendNotification(NotificationConstants.MAINVIEW_REMOVE_DISPLAYOBJECT);
		}
		/**
		 * 监听
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [WinebarConst.GRADE_UP_RESULT,
				NotificationConstants.COMMON_DROP_TIME];
		}
		/**
		 * 捕获
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification :INotification):void
		{
			switch(notification.getName())
			{
				case WinebarConst.GRADE_UP_RESULT://升级
				{
					view.gradeUp();
					break;
				}
				case NotificationConstants.COMMON_DROP_TIME://冷却时间去除
				{
					if(notification.getBody() == BuildingType.BAR)
					{
						AppData.getInstance().winebarManager.wineBarInfoVo.pretime = 0;
						view.dropTime();
					}
					break;
				}
			}
			
		}
		/**
		 * 注册
		 * 
		 */		
		override public function onRegister():void
		{
			//请求信息
			this.sendNotification(ApplicationConstants.WINEBAR_GET_INFO);
			//进入酒吧
			this.sendNotification(ApplicationConstants.WINEBAR_ENTER);
			//界面处理
			if(ResourceManager.instance.isContainResource(ResourceConst.WINEBAR_RESOURCE))
			{
				showUI();
				
			}else
			{
				loader = new QueueLoader();
				var config:XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).winebar[0];
				bytesTotal = int(config.@totalBytes);
				var releaseVersion :String;
				for each(var item:XML in config.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				
				loadRes();	
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			super.onLoadComplete(e);
			showUI();
			ResourceManager.instance.putResource(ResourceConst.WINEBAR_RESOURCE,resList);
		}
		/**
		 * 显示界面 
		 * 
		 */
		private function showUI():void
		{
			viewComponent = Reflection.createInstance("com.lz.football.module.wineBar.view.WineBarView");
			this.sendNotification(NotificationConstants.MAINVIEW_ADD_DISPLAYOBJECT,{type:ModulePanelTypeConst.WINEBAR,dispalyObj:view.bg});
			view.initUI();
			PopUpManager.addWindow(view as DisplayObject,GlobalDef.GameContainer,0,0,false);
			addEventListeners();
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			view.destroyView();
		}
	}
}